package com.lhf.smallClass.service.Impl;

import com.lhf.smallClass.mapper.SmallClassMapper;
import com.lhf.smallClass.model.SmallClass;
import com.lhf.smallClass.model.SmallClassVo;
import com.lhf.smallClass.service.SmallClassService;
import com.lhf.util.AllrecordsVo;
import com.lhf.util.ResultBuliderVo;
import com.lhf.util.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 尐痞猫
 * @date2021-06-23 17:26
 */
@Service
public class SmallClassServiceImpl implements SmallClassService {


    @Autowired
    SmallClassMapper smallClassMapper;

    /*
    2.分页查询接口
     */
    @Override
    public ResultVo queryPage(SmallClassVo smallClassVo) {
        //获得总条数
        int count = smallClassMapper.queryCount(smallClassVo.getCode(), smallClassVo.getName(), smallClassVo.getStatus());
        AllrecordsVo allrecordsVo = new AllrecordsVo();
        //存入当前页
        allrecordsVo.setPageIndex(smallClassVo.getPageIndex());
        //存入每页大小
        allrecordsVo.setPageSize(smallClassVo.getPageSize());
        //存入总条数
        allrecordsVo.setTotalPage(count);
        //根据总条数获得总页数
        allrecordsVo.resettotalNum(count);
        List<SmallClass> smallClasses = smallClassMapper.queryPage(smallClassVo.getCode(), smallClassVo.getName(), smallClassVo.getStatus(),smallClassVo.getPageIndex(),smallClassVo.getPageSize());
        allrecordsVo.setDataList(smallClasses);
        ResultVo resultVo = new ResultVo();
        if (!smallClasses.isEmpty()) {
            return ResultBuliderVo.success(allrecordsVo);
        } else {
            return ResultBuliderVo.error("未查询到数据");
        }

    }
    /*
    3.新增接口
     */
    @Override
    public ResultVo add(SmallClass smallClass) {
        int result = smallClassMapper.queryByCode(smallClass.getCode());
        int result2 = smallClassMapper.queryByName(smallClass.getName());
        if (result != 0 || result2 != 0) {
            return ResultBuliderVo.error("编号或名称不能重复");
        } else {
            smallClass.getstatusName(smallClass.getStatus());
            smallClassMapper.add(smallClass);
            return ResultBuliderVo.success(null);
        }
    }
    /*
    4.修改回显接口
     */
    @Override
    public ResultVo queryById(Integer id) {
        SmallClass smallClass = smallClassMapper.queryById(id);
        if (smallClass == null) {
            return ResultBuliderVo.error("未查询到数据");
        } else {
            smallClass.getstatusName(smallClass.getStatus());
            return ResultBuliderVo.success(smallClass);
        }
    }
    /*
    5.修改接口
     */
    @Override
    public ResultVo update(SmallClass smallClass) {
        int result = smallClassMapper.queryByName(smallClass.getName());
        if (result == 0) {
            smallClass.getstatusName(smallClass.getStatus());
            int update = smallClassMapper.update(smallClass);
            return ResultBuliderVo.success(null);
        }else {
            return ResultBuliderVo.error("名称重复");
        }
    }
    /*
    6.启用禁用接口
     */
    @Override
    public ResultVo updateByStatus(SmallClass smallClass) {
        smallClass.getstatusName(smallClass.getStatus());
        int result = smallClassMapper.updateByStatus(smallClass);
        if (result == 0) {
            return ResultBuliderVo.error("");
        }else {
            return ResultBuliderVo.success(null);
        }
    }
    /*
    7.相应所有启用备件小类接口
     */
    @Override
    public ResultVo queryByStatus(Integer status) {
        List<SmallClass> smallClasses = smallClassMapper.queryByStatus(status);
        if(!smallClasses.isEmpty()){
            return ResultBuliderVo.success(smallClasses);
        }
        return ResultBuliderVo.error("未查询到信息");
    }
    /*
    1.ajax回显状态栏参数
     */
    @Override
    public ResultVo queryAllStatus() {
        List<SmallClass> smallClasses = smallClassMapper.queryAllStatus();
        if(!smallClasses.isEmpty()){
            return ResultBuliderVo.success(smallClasses);
        }
        return ResultBuliderVo.error("未查询到信息");
    }


}
